package com.naiterui.ehp.bs.recommend.controller.medication;


import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.constants.SwaggerTags;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bs.recommend.exception.ExceptionCodes;
import com.naiterui.ehp.bs.recommend.service.IPrescriptionService;
import com.naiterui.ehp.bs.recommend.swagger.Tags;
import com.naiterui.ehp.bs.recommend.vo.prescription.PatientPrescriptionVO;
import com.naiterui.ehp.bs.recommend.vo.prescription.PrescriptionDetailVO;
import com.naiterui.ehp.bs.recommend.vo.prescription.PrescriptionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 处方笺相关
 *
 * @Author wangchuan
 * @Date 2017年7月28日
 * @since V2.9.0
 */
@Api(tags = SwaggerTag.DOCTOR + SwaggerTag.RECOM)
@Controller
@RequestMapping(value = "recommend/prescription")
public class PrescriptionController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PrescriptionController.class);

    @Autowired
    private IPrescriptionService prescriptionService;

    /**
     * 推药处方笺初始化
     *
     * @param doctorId  医生ID
     * @param patientId 患者ID
     *
     * @return
     *
     * @throws BusinessException
     * @Description
     * @author wangchuan
     * @Date 2017年7月21日
     * @since V2.9.0
     */

    @RequestMapping("init")
    @ApiOperation(tags = {SwaggerTags.APP_DOCTOR_INQUIRER}, value = "推药处方笺初始化", notes = "推荐用药时，处方笺页初始化", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "inquirerId", value = "就诊人id", required = true, dataType = "long", paramType = "query")})
    @ResponseBody
    public ResponseVO<PrescriptionVO> initPrescription(Long doctorId, Long patientId, Long inquirerId) throws BusinessException {
        LOGGER.info("推药处方笺初始化 doctorId={}, patientId={}， inquirerId={}", doctorId, patientId, inquirerId);
        if (doctorId == null || patientId == null || inquirerId == null) {
            LOGGER.info("推药处方笺初始化，医生ID或患者ID为空");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        PrescriptionVO prescription = prescriptionService.getPrescription(doctorId, patientId, inquirerId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, prescription);
    }

    /**
     * 医生端处方笺详情
     *
     * @param doctorId    医生ID
     * @param patientId   患者ID
     * @param recommendId 推荐单ID
     *
     * @return
     *
     * @throws BusinessException
     * @Description
     * @author wangchuan
     * @Date 2017年7月31日
     * @since V2.9.0
     */

    @RequestMapping("detail")
    @ApiOperation(tags = { SwaggerTags.APP_DOCTOR_INQUIRER }, value = "医生端获取处方笺详情", notes = "获取处方笺详情", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "recommendId", value = "推荐单ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "showExpire", value = "是否显示到期提示", dataType = "boolean", paramType = "query") })
    @ResponseBody
    public ResponseVO<PrescriptionDetailVO> getPrescriptionDetail(Long doctorId, Long patientId, Long recommendId, Boolean showExpire)
            throws BusinessException {
        LOGGER.info("医生端处方笺详情 doctorId={}, patientId={}, recommendId={}, showExpire={}", doctorId, patientId, recommendId,
                showExpire);
        if (doctorId == null || patientId == null || recommendId == null) {
            LOGGER.info("获取处方笺详情，参数错误");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        if (showExpire == null) {
            showExpire = false;
        }
        PrescriptionDetailVO prescription = prescriptionService.getPrescriptionDetailForDoctor(doctorId, patientId, recommendId, showExpire);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, prescription);
    }

    /**
     * 患者端获取处方笺详情
     *
     * @param doctorId
     * @param patientId
     * @param recommendId
     *
     * @return
     *
     * @throws BusinessException
     */

    @RequestMapping("/patient/detail")
    @ApiOperation(tags = { Tags.VERSION290, Tags.APP }, value = "患者端获取处方笺详情", notes = "患者端获取处方笺详情", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "doctorId", value = "医生ID", dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "recommendId", value = "推荐单ID", required = true, dataType = "long", paramType = "query") })
    @ResponseBody
    public ResponseVO<PatientPrescriptionVO> getPtPrescriptionDetail(Long doctorId, Long patientId, Long recommendId)
            throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        LOGGER.info("患者端获取处方笺详情 doctorId={}, patientId={}, recommendId={}", doctorId, patientId, recommendId);
        if (patientId == null || recommendId == null) {
            LOGGER.info("患者端获取处方笺详情，参数错误");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        PatientPrescriptionVO prescription = prescriptionService.getPrescriptionDetailForPatient(doctorId, patientId, recommendId);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS, prescription);
    }

}
